/*
 * Copyright (C) 2022-2024 Huawei Device Co., Ltd.
 * Licensed under the MIT License, (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Handler, HandlerOptions } from './Handler';
import HashMap from '@ohos.util.HashMap';
import ArrayList from '@ohos.util.ArrayList';
import { References } from './References';
import { MethodParameters } from '../entry/MethodParameters';
import { LogUtil } from '../../../../utils/LogUtil';


export class MetadataReader {
  private static sInstance: MetadataReader;
  private handleMap: HashMap<String, ArrayList<MethodParameters>>;
  private referencesMap: HashMap<String, References>;

  private constructor() {
    LogUtil.info('MetadataReader constructor called');
    this.handleMap = new HashMap<String, ArrayList<MethodParameters>>();
    this.referencesMap = new HashMap<String, References>();
  }

  public static getInstance(): MetadataReader {
    if (!this.sInstance) {
      this.sInstance = new MetadataReader();
    }
    return this.sInstance;
  }

  public setSubHandle(className: String, methodName: String, options: HandlerOptions) {
    if (!className || className.trim().length == 0) {
      throw new Error("the listener no get className to handle");
    }
    let handle = new Handler(options);
    var methodPa: ArrayList<MethodParameters> = new ArrayList();
    try {
      if (this.handleMap.hasKey(className)) {
        methodPa = this.handleMap.get(className);
      }

      var isSample: boolean = false;
      if (methodPa.length > 0) {
        methodPa.forEach((value, index) => {
          if (value.getMethodName() == methodName) {
            isSample = true;
          }
        })
      }
      if (!isSample) {
        methodPa.add(new MethodParameters(methodName, handle));
      }
      this.handleMap.set(className, methodPa);
    } catch (e) {
      LogUtil.error(e);
    }
  }

  public setListenerReferencesType(className: String, reType: References): void {
    if (!reType) {
      return;
    }
    if (!className || className.trim().length == 0) {
      throw new Error('the listener no get className to handle');
    }
    try {
      if (this.referencesMap.hasKey(className)) {
        this.referencesMap.remove(className);
      }
      this.referencesMap.set(className, reType);
    } catch (e) {
      LogUtil.error(e);
    }
  }

  public getMethodHandle(className: String): ArrayList<MethodParameters> | undefined {
    if (!className || className.trim().length == 0) {
      return undefined;
    }
    try {
      if (this.handleMap.hasKey(className)) {
        return this.handleMap.get(className);
      }
    } catch (e) {
      LogUtil.error(e);
    }
    return undefined;
  }

  getResType(className: String): References {
    if (!className || className.trim().length == 0) {
      return References.Strong;
    }
    try {
      if (this.referencesMap.hasKey(className)) {
        return this.referencesMap.get(className);
      }
    } catch (e) {
      LogUtil.error(e);
    }
    return References.Strong;
  }

  useStrongReferences(className: String): boolean {
    LogUtil.debug(`useStrongReferences called for className: ${className}`);
    let resType = this.getResType(className);
    return !resType ? true : resType == References.Strong.valueOf();
  }
}